import request from '@/utils/request'

/**
* 根据频道获取频道
* @param {Number} channelId - 频道ID
* @param {Number} timestamp - 时间戳
* @returns
*/
export const getArticlesByChannel = (channelId, timestamp) => {
  return request({
    url: '/articles',
    method: 'get',
    params: {
      channel_id: channelId,
      timestamp
    }
  })
}

/**
* 获取文章详情
* @param {String} id - 文章ID
* @returns
*/
export const getArticle = (id) => {
  return request({
    url: '/articles/' + id
  })
}

/**
 * 文章点赞
 */
export function likeArticle (articleId, isLike) {
  if (isLike) {
    // 点赞
    return request({
      url: '/article/likings',
      method: 'post',
      data: { target: articleId }
    })
  } else {
    // 取消点赞
    return request({
      url: '/article/likings/' + articleId,
      method: 'delete'
    })
  }
}

/**
 * 文章收藏
 *
 */
export function collectArticle (article, isCollect) {
  if (isCollect) {
    // 取消收藏
    return request({
      url: '/article/collections/' + article,
      method: 'delete'
    })
  } else {
    // 收藏
    return request({
      url: '/article/collections',
      method: 'post',
      data: { target: article }
    })
  }
}

/**
* 对文章进行评论，对评论进行回复
* @param {*} target - 评论：文章ID，回复：评论ID
* @param {*} content - 内容
* @param {*} articleId - 回复：文章ID
* @returns
*/
export const commentOrReply = (target, content, articleId = null) => {
  return request({
    url: '/comments',
    method: 'post',
    data: { target, content, art_id: articleId }
  })
}
